package com.intel.bluetooth;

import com.mlnx.mptp.model.ecg.ECGChannelType;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * Created by amanda.shan on 2020/8/20.
 */
public class EcgBluFrame extends JFrame {


    private ConnectService connectService;

    private JComboBox comboBox;

    private JTextField textField;

    public EcgBluFrame() throws HeadlessException {

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        setSize(500, 400);
        setLocation(400, 300);

        init();
    }

    private void init() {

        JPanel contentPane = new JPanel();//初始化面板
        contentPane.setLayout(null);//设置布局NULL

        JButton connectButton = new JButton("连接");
        connectButton.setBounds(50, 50, 90, 30);
        contentPane.add(connectButton);
        connectButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String mac = textField.getText();
                if (mac.isEmpty() || mac == null) {
                    JOptionPane.showMessageDialog(EcgBluFrame.this, "输入mac地址");
                    return;
                }
                connectService = new ConnectService(mac);
                connectService.connectDevice();
            }
        });

        textField = new JTextField("000666D0D9A7");
        textField.setBounds(150, 50, 120, 30);
        contentPane.add(textField);

        JButton startButton = new JButton("开始检测");
        startButton.setBounds(50, 100, 90, 30);
        contentPane.add(startButton);
        startButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                connectService.start();
            }
        });

        JButton stopButton = new JButton("停止检测");
        stopButton.setBounds(50, 150, 90, 30);
        contentPane.add(stopButton);
        stopButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                connectService.stop();
            }
        });

        JButton configButton = new JButton("配置导联数量");
        configButton.setBounds(50, 200, 110, 30);
        contentPane.add(configButton);
        configButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                connectService.config(ECGChannelType.valueOf((String) comboBox.getSelectedItem()));
            }
        });

        comboBox = new JComboBox(new String[]{"CHAN_1", "CHAN_3", "CHAN_8"});
        comboBox.setBounds(200, 200, 80, 30);
        contentPane.add(comboBox);

        setContentPane(contentPane);
    }

    public static void main(String[] args) {

        EcgBluFrame ecgBluFrame = new EcgBluFrame();
        ecgBluFrame.setVisible(true);
    }
}
